﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TLang.Ast
{
    using Values;

    public abstract class Node
    {
        public readonly String file;
        public readonly int start;
        public readonly int end;
        public readonly int line;
        public readonly int col;


        protected Node(String file, int start, int end, int line, int col)
        {
            this.file = file;
            this.start = start;
            this.end = end;
            this.line = line;
            this.col = col;
        }


        public abstract Value Interp(Scope s);


        public static Value Interp(Node node, Scope s)
        {
            return node.Interp(s);
        }


        public abstract Value Typecheck(Scope s);


        public static Value Typecheck(Node node, Scope s)
        {
            return node.Typecheck(s);
        }


        public static List<Value> InterpList(IEnumerable<Node> nodes, Scope s)
        {
            List<Value> values = new List<Value>();
            foreach (Node n in nodes)
            {
                values.Add(n.Interp(s));
            }
            return values;
        }


        public static List<Value> TypecheckList(IEnumerable<Node> nodes, Scope s)
        {
            List<Value> types = new List<Value>();
            foreach (Node n in nodes)
            {
                types.Add(n.Typecheck(s));
            }
            return types;
        }


        public String GetFileLineCol()
        {
            return file + ":" + (line + 1) + ":" + (col + 1);
        }


        public static String PrintList(IEnumerable<Node> nodes)
        {
            StringBuilder sb = new StringBuilder();
            bool first = true;
            foreach (Node e in nodes)
            {
                if (!first)
                {
                    sb.Append(" ");
                }
                sb.Append(e);
                first = false;
            }
            return sb.ToString();
        }
    }
}
